package org.ithink.util;

import java.math.BigDecimal;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Convert {

	/**
	 * Long类型转换
	 */
	public static Long toLong(String str){
		Long i = null;
		try {
			if(NullsUtil.isNotEmpty(str)){
				i = new Long(str);
			}
		} catch (Exception e) {
		}
		return i;
	}
	
	public static Long toLong(String str, long defaultVal){
		Long i = new Long(defaultVal);
		try {
			if(NullsUtil.isNotEmpty(str)){
				i = new Long(str);
			}
		} catch (Exception e) {
		}
		return i;
	}
	
	/**
	 * BigDecimal类型转换：转不了，返回null
	 */
	public static BigDecimal toBigDecimal(String str){
		BigDecimal bd = null;
		try {
			if(NullsUtil.isNotEmpty(str)){
				bd = new BigDecimal(str);
			}
		} catch (Exception e) {
		}
		return bd;
	}
	
	/**
	 * BigDecimal类型转换：转不了，抛异常
	 */
	public static BigDecimal toBigDecimal(String str, String expMsg){
		BigDecimal bd = null;
		try {
			if(NullsUtil.isNotEmpty(str)){
				bd = new BigDecimal(str);
			} else {
				throw new RuntimeException(expMsg);
			}
		} catch (Exception e) {
			throw new RuntimeException(expMsg);
		}
		return bd;
	}
	
	/**
	 * BigDecimal类型转换：转不了，默认值
	 */
	public static BigDecimal toBigDecimal(String str, double defaultVal){
		BigDecimal bd = new BigDecimal(defaultVal);
		try {
			if(NullsUtil.isNotEmpty(str)){
				bd = new BigDecimal(str);
			}
		} catch (Exception e) {
		}
		return bd;
	}
	
	/**
	 * Short类型转换
	 */
	public static Short toShort(String str){
		Short st = null;
		if(null != str && !"".equals(str)){
			st = new Short(str);
		}
		return st;
	}
	
	/**
	 * Integer类型转换
	 */
	public static Integer toInteger(String str){
		Integer i = null;
		if(null != str && !"".equals(str)){
			i = new Integer(str);
		}
		return i;
	}
	/**
	 * Float类型转换
	 */
	public static Float toFloat(String str){
		Float i = null;
		if(null != str && !"".equals(str)){
			i = new Float(str);
		}
		return i;
	}
	/**
	 * Float类型转换
	 */
	public static Float toFloat(String str,float defaultVal){
		Float i = defaultVal;
		if(null != str && !"".equals(str)){
			i = new Float(str);
		}
		return i;
	}
		
	/**
	 * Date类型转换
	 */
	public static Date toParseDate(String time)  {
		/** */
		/**
		 * 字符串转换为java.util.Date 支持格式为:
		 * yyyy.MM.dd G 'at' hh:mm:ss z 如 '2002-1-1 AD at 22:10:59 PSD' 
		 * yy/MM/dd HH:mm:ss 如 '2002/1/1 17:55:00' 
		 * yy/MM/dd HH:mm:ss pm 如 '2002/1/1 17:55:00 pm' 
		 * yy-MM-dd HH:mm:ss 如 '2002-1-1 17:55:00' 
		 * yy-MM-dd HH:mm:ss am 如 '2002-1-1 17:55:00 am'
		 * 
		 * @param time
		 *            String 字符串
		 * @return Date 日期
		 */
		try
		{
		SimpleDateFormat formatter;
		
		int tempPos = time.indexOf("AD");
		time = time.trim();
		formatter = new SimpleDateFormat(" yyyy.MM.dd G 'at' hh:mm:ss z ");
		if (tempPos > -1) {
			time = time.substring(0, tempPos) + " 公元 "
			+ time.substring(tempPos + " AD ".length()); // china
			formatter = new SimpleDateFormat(" yyyy.MM.dd G 'at' hh:mm:ss z ");
		}
		
		tempPos = time.indexOf("-");
		
		if (time.indexOf(".") != -1) {
			time = time.substring(0, time.indexOf("."));
		}
		
		if (tempPos > -1) {//包含“-”
			if(time.indexOf(":") == -1){
				formatter = new SimpleDateFormat("yyyy-MM-dd");
			} else if (time.indexOf(":") == time.lastIndexOf(":")) {
				formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			} else {
				formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			}
		} else if (time.indexOf("/") > -1) {//包含“/”
			if(time.indexOf(":") == -1){
				formatter = new SimpleDateFormat("yyyy/MM/dd");
			}else{
				formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			}
		} else if (time.substring(0, 8).indexOf("-") == -1) {
			time = time.substring(0, 8);
			formatter = new SimpleDateFormat("yyyyMMdd");
		} else {
			formatter = new SimpleDateFormat("HH:mm");
		}
		
		ParsePosition pos = new ParsePosition(0);
		java.util.Date ctime = formatter.parse(time, pos);
		return ctime;
		
		}
		catch(Exception ex)
		{ return null;}
		
	}
	
	/**
	 * DataFormat
	 */
	public static String dataFormat(Date date, String pattern){
		SimpleDateFormat df = new SimpleDateFormat(pattern);
		try {
			return df.format(date);
		} catch (Exception e) {
			return "";
		}
		
	}
	
	/**
	 * 获取当前日期第一天
	 */
	public static Date getFirstDayByMonth(Date date) throws Exception{
		Calendar cal = Calendar.getInstance();   
		cal.setTime(date);   
		int stDate = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-");
		String startDate1 = sdf.format(date).concat(String.valueOf(stDate));
		return toParseDate(startDate1);
	}
	
	/**
	 * 获取当前提前最后一天
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date getLastDayByMonth(Date date) throws Exception{
		Calendar cal = Calendar.getInstance();   
		cal.setTime(date);   
		int enDate = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-");
		String endDate1 = sdf.format(date).concat(String.valueOf(enDate));
		return toParseDate(endDate1);
	}
	
	/**
	 * 获取当前日期下一天
	 */
	public static Date getNexDate(Date date)throws Exception{
		Calendar calendar=Calendar.getInstance();   
		calendar.setTime(date);   
		int day=calendar.get(Calendar.DATE);   
		calendar.set(Calendar.DATE,day+1);
		Date dat = calendar.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
		String  date1   = sdf.format(dat) ; 
		Date d = sdf.parse(date1);
		return d;
	}
	/**
	 * 获取当前日期前一天
	 */
	public static Date getForwardOneDate(Date date){
		try {
			Calendar calendar = Calendar.getInstance();   
			calendar.setTime(date);   
			int day=calendar.get(Calendar.DATE);   
			calendar.set(Calendar.DATE,day - 1);
			Date dat = calendar.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
			String  date1  = sdf.format(dat) ; 
			Date d = sdf.parse(date1);
			return d;
		} catch (Exception e) {
			return null;
		}
	}
	/**
	 * 获取当前日期前10天
	 */
	public static Date getForwardTenDate(Date date){
	  try {
	    Calendar calendar = Calendar.getInstance();   
	    calendar.setTime(date);   
	    int day=calendar.get(Calendar.DATE);   
	    calendar.set(Calendar.DATE,day - 10);
	    Date dat = calendar.getTime();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
	    String  date1  = sdf.format(dat) ; 
	    Date d = sdf.parse(date1);
	    return d;
    } catch (Exception e) {
      return null;
    }
		
	}
	/**
	 * 获取当前日期前几天
	 */
	public static Date getForwardSumeDate(Date date,int days){
		try {
			Calendar calendar = Calendar.getInstance();   
			calendar.setTime(date);   
			int day=calendar.get(Calendar.DATE);   
			calendar.set(Calendar.DATE,day - days);
			Date dat = calendar.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
			String  date1  = sdf.format(dat) ; 
			Date d = sdf.parse(date1);
			return d;
		} catch (Exception e) {
			return null;
		}
		
	}
	/**
	 * 获取当前日期后7天
	 */
	public static Date getSevenDate(Date date){
	  try {
	    Calendar calendar = Calendar.getInstance();   
	    calendar.setTime(date);   
	    int day=calendar.get(Calendar.DATE);   
	    calendar.set(Calendar.DATE,day + 6);
	    Date dat = calendar.getTime();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
	    String  date1  = sdf.format(dat) ; 
	    Date d = sdf.parse(date1);
	    return d;
    } catch (Exception e) {
      return null;
    }
		
	}
	
	/**
	 * 处理当前日期去掉小时分钟秒
	 */
	public static Date getCurrentDate(Date date)throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
		String  date1   = sdf.format(date) ; 
		Date d = sdf.parse(date1);
		return d;
	}
	
	public static void main(String[] args){
		System.out.println(Long.MAX_VALUE);
		System.out.println(toInteger("1003494011"));
	}
	
}
